home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / struct.dqc / struct.doc
Encoding:
Text File  |  1985-07-03  |  4.4 KB  |  161 lines

  1.  
  2.  
  3.  
  4.  
  5.                           C STRUCTURE DOCUMENTATION
  6.                                      or
  7.                       Things Your Books Never Told You
  8.  
  9.                               By Mark Grennan
  10.                              6204 Reeves Court
  11.                           Oklahoma City, Ok 73122
  12.                             (405) 728-2463 DATA
  13.                             (405) 728-9836 VOICE
  14.                                    7/3/85
  15.  
  16.  
  17.     If you are developing large applications in C and using lots of file
  18. structures there are a few things you should now, that I have not found
  19. and any of the books. These things may work differently in your compiler,
  20. but I feel you should make your self aware of them.
  21.  
  22.     ITEM 1: When you declare a structure, its member names must be mutually
  23.         distinct. This is documented in the K&R book pate 197, 
  24.         paragraph four. But is easily overlooked. Let me give you
  25.         an example....
  26.  
  27. struct TESTA {
  28.     char fname[20];
  29.     char lname[20];
  30.     } member;
  31.  
  32. struct TESTB {
  33.     char lname[20];
  34.     char fname[20];
  35.     } user;
  36.  
  37.         If your program declares two or more structers both with similar
  38.         member names, your compiler (according to K&R) should give you
  39.         a compiler error. Some compilers have gone beyond this, as K&R
  40.         sagest in their book at later versions of C will do. If yours
  41.         dos your lucky. 
  42.  
  43.  
  44.     ITEM 2: Lets say you declare you structures right and use unek names.
  45.         But, you wish to have each of you records work out to be one
  46.         of the magic numbers (8, 16, 32 ...) in size, so you throw in
  47.         a filler[ variable ] to pad each each record with. Example...
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. è
  56. struct TESTA {
  57.         char filler[24];
  58.     char mem_fname[20];
  59.     char mem_lname[20];
  60.     } member;
  61.  
  62. struct TESTB {
  63.         char filler[4];
  64.     char usr_fname[20];
  65.     char usr_lname[20];
  66.         char addr[20];
  67.     } user;
  68.  
  69.         Your compiler will probably not give you any error on this.
  70.         Your member names are all either unek or your members have 
  71.         the same name and offset. In this case the first member of 
  72.         the structure (OFFSET 0). I use Computer Innovations C86 and 
  73.         it doesn't give an error on these declaration.
  74.  
  75.         NOW THE RUB. As the compiler parses the code and creates
  76.         space in memory for you structers it confuses usr.filler with
  77.         member.filler and assigens the size of structer user to be 84
  78.         bytes long. That is the 24 bytes from member and the rest of
  79.         your structure. This is not a large problem, just wasteful
  80.         you say. Ok, turn the structures over. Put user on top and
  81.         member on bottom. Now what happens? Member.filler gets declared
  82.         as a length of 4 not 24. So, you write you data into it and over
  83.         write come piece of code lying after it in memory. OOOOOOHH!!!
  84.         EXAMPLE..... (I ran this program threw my compiler, asking it
  85.         it give me assembly code output)
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. typedef struct TESTA {
  94.     char s1[128];
  95.     char s2[128];
  96.     };
  97. typedef struct TESTB {
  98.     char s1[16];
  99.     char s3[16];
  100.     };
  101. typedef struct TESTC {
  102.     char s1[32];
  103.     char s4[32];
  104.     };
  105.  
  106. struct TESTA testa;
  107. struct TESTB testb;
  108. struct TESTC testc;
  109. int fp;è
  110. main()
  111. {
  112.     int x;
  113.  
  114.     strcpy(testb.s1,"123456789012345");
  115.     strcpy(testb.s2,"ABCDEFGHIJKLMNO");
  116.         
  117.     fp = fopen("example.txt","wr");
  118.         for(x=0; x<10; x++) {
  119.         fseek(fp,0L,2);
  120.       printf("\nWritting record at file address %d",ftell(fp));
  121.       fwrite(&testb,sizeof(struct TESTB),1,fp);
  122.     }
  123. }
  124.  
  125.  
  126.  
  127.         And here's what I got in my data segment.
  128.  
  129.  
  130.  
  131. @DATAU    SEGMENT
  132.     db    562 DUP (?)
  133.  
  134.     ORG    0        ;Start of segement
  135. TESTA    LABEL    WORD        ;create label for structure TESTA
  136.  
  137.     public    TESTA
  138.     ORG    256        ;Set address for next structure leaving
  139. TESTB    LABEL    WORD        ; 256 bytes for the first (that's right)
  140.                 ;Now create space for the next structure
  141.     public    TESTB        ;which should be 32 bytes long. SO 
  142.     ORG    400        ; 256 + 32 = 288, But we have 400 that's
  143. TESTC    LABEL    WORD        ; 256 + 128 + 16.
  144.  
  145.     public    TESTC        ; This should be 400 (it it was right) + 64
  146.     ORG    560        ; but its 400 + 128 + 32.
  147. FP    LABEL    WORD
  148.  
  149.     public    FP
  150.     public    MAIN
  151. @DATAU    ENDS
  152.  
  153.  
  154.  
  155.         So you can use similar member names in different structures IF
  156.         they start in the same offset and have the same length.
  157.  
  158.     I hope this will help you with your development. It took a while for me
  159. to get over my ignorance and get some of the programs I'm writing to work.
  160. Good luck with yours.
  161.